package com.softdev.common.config;

import com.softdev.common.security.fliter.JwtAuthenticationTokenFilter;
import com.softdev.common.security.handler.CustomAccessDeniedHandler;
import com.softdev.common.security.handler.UnAuthorizedEntryPoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled=true,securedEnabled=true,jsr250Enabled = true)
public class SecurityConfig {

    @Resource
    private UserDetailsService userDetailsService;

    @Resource
    private CustomAccessDeniedHandler customAccessDeniedHandler;

    @Resource
    private UnAuthorizedEntryPoint unAuthorizedEntryPoint;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity https) throws Exception {
        return https.csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .formLogin()
                .loginPage("/index.html")
                .and()
                .authorizeHttpRequests()
                .antMatchers(HttpMethod.GET,
                        "/ws/**",
                        "/topic/*",
                        "/app/*",

                        "/doc.html",
                        "/webjars/**",
                        "/swagger-resources",
                        "/v3/api-docs",
                        "/favicon.ico"
                )
                .permitAll()
                .antMatchers(
                        "/user/login"
                )
                .permitAll()
                .anyRequest()
                .authenticated()
//                .authorizeRequests().anyRequest().permitAll()
                .and()
                .headers().frameOptions().disable()
                .and()
                .addFilterBefore(jwtAuthenticationTokenFilter() , UsernamePasswordAuthenticationFilter.class)
                .exceptionHandling()
                .accessDeniedHandler(customAccessDeniedHandler)
                .authenticationEntryPoint(unAuthorizedEntryPoint)
                .and()
                .userDetailsService(userDetailsService)
                .cors()
//                .configurationSource(configurationSource())
                .and()
                .build();
    }

    @Bean
    public JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter() {
        return new JwtAuthenticationTokenFilter();
    }

//    CorsConfigurationSource configurationSource() {
//        CorsConfiguration corsConfiguration = new CorsConfiguration();
//        corsConfiguration.setAllowedHeaders(Collections.singletonList("*"));
//        corsConfiguration.setAllowedMethods(Collections.singletonList("*"));
//        corsConfiguration.setAllowedOrigins(Arrays.asList("http://localhost:8080" , "http://localhost:3000"));
//        corsConfiguration.setAllowCredentials(true);
//        corsConfiguration.setExposedHeaders(Collections.singletonList("Access-Control-Allow-Origin"));
//        corsConfiguration.setMaxAge(3600L);
//
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**" , corsConfiguration);
//        return source;
//    }

}
